package com.dell.optimize;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class TimerService extends Service 
{
    
    
    private Timer timer=new Timer();    

    private static long UPDATE_INTERVAL = 15*60*1000;  //default

    private static long DELAY_INTERVAL = 0;  
    
    Optimize rec ;
        
    /* 
     * not using ipc...but if we use in future
     */
    public IBinder onBind(Intent intent) {
      return null;
    }

    @Override 
    public void onCreate() 
    {
      super.onCreate();     

      _startService();

    }

    @Override 
    public void onDestroy() 
    {
      super.onDestroy();
      _shutdownService();
      stopService(new Intent(this, OptimizationService.class));
     /* try {
			rec.stop();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(" Exception while Destroying :"+e);
		}*/

    }

    
    /*
     * starting the service
     */
    private void _startService()
    {      
      timer.scheduleAtFixedRate(    
          
              new TimerTask() {

                    public void run() {
                        doServiceWork();
                    }
                  },
                  DELAY_INTERVAL,
                  UPDATE_INTERVAL);

      Log.i(getClass().getSimpleName(), "FileScannerService Timer started....");
    }
    
    /*
     * start the processing, the actual work, getting config params, get data from network etc
     */
    private void doServiceWork()
    {/*
    	if(rec != null){
    		try {
				rec.stop();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	rec  = new Optimize(this, "optimizerLog");
		try {
			
			rec.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(" Exception while Creating : "+e);
		}
        
    */
    	stopService(new Intent(this, OptimizationService.class));
    	// garbage collection 
    	//System.gc();
    	startService(new Intent(this, OptimizationService.class));
    	
    }
    
    /*
     * shutting down the service
     */
    private void _shutdownService()
    {
      if (timer != null) timer.cancel();
    }
}